package com.jichangxiu.server.platform.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.server.platform.entity.dto.PlatformRoleQueryDto;
import com.jichangxiu.server.platform.entity.vo.PlatformRoleAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformRoleAuthEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformRoleEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformRoleQueryVo;
import com.jichangxiu.server.platform.service.PlatformRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * PlatformRoleAction:
 *
 * @author Jcx
 * @create 2023-03-21 19:49
 */
@Slf4j
@Rest("/platform/role")
@Tag(name = "平台-角色管理控制器")
public class PlatformRoleAction extends BaseAction {

    @Resource
    private PlatformRoleService platformRoleService;

    /**
     * 新增角色
     *
     * @param platformRoleAddVo 角色视图对象
     * @return 是否成功
     */
    @Post("/add")
    @Operation(summary = "平台-新增角色")
    @PreAuthorize("@ss.hasPermission('platform:role:add')")
    public String add(@RequestBody PlatformRoleAddVo platformRoleAddVo) {
        return toStr(platformRoleService.add(platformRoleAddVo));
    }

    /**
     * 多选级联删除角色
     *
     * @param roleIds 角色ID列表
     * @return 是否成功
     */
    @Delete("/del")
    @Operation(summary = "平台-多选级联删除角色")
    @PreAuthorize("@ss.hasPermission('platform:role:del')")
    public String del(@RequestBody String[] roleIds) {
        return toStr(platformRoleService.del(ListUtil.toList(roleIds)));
    }

    /**
     * 修改角色信息
     *
     * @param platformRoleEditVo 角色视图对象
     * @return 是否成功
     */
    @Put("/edit")
    @Operation(summary = "平台-修改角色信息")
    @PreAuthorize("@ss.hasPermission('platform:role:edit')")
    public String edit(@RequestBody PlatformRoleEditVo platformRoleEditVo) {
        return toStr(platformRoleService.edit(platformRoleEditVo));
    }

    /**
     * 修改角色权限信息
     *
     * @param platformRoleAuthEditVo 角色权限视图对象
     * @return 是否成功
     */
    @Put("/updateRoleMenuAndDataAuth")
    @Operation(summary = "平台-修改角色权限信息")
    @PreAuthorize("@ss.hasPermission('platform:role:updateRoleMenuAndDataAuth')")
    public String updateRoleMenuAndDataAuth(@RequestBody PlatformRoleAuthEditVo platformRoleAuthEditVo) {
        return toStr(platformRoleService.updateRoleMenuAndDataAuth(platformRoleAuthEditVo));
    }

    /**
     * 根据ID获取角色详情
     *
     * @param roleId 角色ID
     * @return 角色详情
     */
    @Get("/info/{roleId}")
    @Operation(summary = "平台-根据ID获取角色详情")
    @PreAuthorize("@ss.hasPermission('platform:role:info')")
    public PlatformRoleQueryDto info(@PathVariable("roleId") @NotBlank(message = "角色ID不能为空") String roleId) {
        return platformRoleService.info(roleId);
    }

    /**
     * 分页查询角色列表
     *
     * @param platformRoleQueryVo 角色视图对象
     * @return 分页角色列表
     */
    @Get("/pageList")
    @Operation(summary = "平台-分页查询角色列表")
    @PreAuthorize("@ss.hasPermission('platform:role:pageList')")
    public PageData<PlatformRoleQueryDto> pageList(@ParameterObject PlatformRoleQueryVo platformRoleQueryVo) {
        return platformRoleService.pageList(platformRoleQueryVo);
    }

}
